<template>
  <div id="bigDiv">
    <special />
    <div id="fanhui"><i class="el-icon-back"></i></div>
    <div id="tittle">登 录</div>
    <div id="touxiang">
      <img src="https://pic1.imgdb.cn/item/6360bb6716f2c2beb12e6cb8.png" alt="" />
    </div>
    <div id="middle">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="65px" class="demo-ruleForm">
        <el-form-item label="账号:" prop="number" style="font-weight: bold">
          <el-input v-model.number="ruleForm.number"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="pass" style="font-weight: bold">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button type="primary" @click="goResiger">注册</el-button>
        </el-form-item>
      </el-form>
      <div id="mingyan">
        <dd>数学是一切知识中的最高形势。</dd>
        <br />
        <dt style="text-align: right">—— 柏拉图</dt>
      </div>
    </div>
  </div>
</template>

<script>
import aes from "@/utils/aes";
import Special from "@/components/Special/special.vue";
import {getUser} from "@/api/users"
export default {
  components: { Special },
  data() {
    var checkNumber = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      setTimeout(() => {
        // if (!Number.isInteger(value)) {
        //   callback(new Error("请输入数字值"));
        // } else {
        //   if (value < 18) {
        //     callback(new Error("必须年满18岁"));
        //   } else {
        //     callback();
        //   }
        // }
        callback()
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      callback();
    };
    return {
      ruleForm: {
        pass: "",
        number: "",
      },
      useInfo:[],
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        number: [{ validator: checkNumber, trigger: "blur" }],
      },
    };
  },
  created(){
    console.log(sessionStorage.getItem("isWho")) 
    getUser().then((res)=>{
      this.useInfo=res
      console.log(this.useInfo,888,res)
    }).catch((error)=>{
      console.log(error)
    })
  },
  methods: {
    goResiger() {
      this.$router.push("/resiger")
      console.log(13513513513233)
    },

    submitForm(formName) {
      let newThis = this;
      let flag = false;
      this.useInfo.forEach((item) => {
        console.log(item.userName,item.passworld)
        if (item.userName == newThis.ruleForm.number && item.passworld == newThis.ruleForm.pass) {
          if (!sessionStorage.token) {
            const uuid = require('uuid')
            let encryptPassword = aes.encrypt(uuid.v1());
            sessionStorage.setItem("userName", item.userName)
            sessionStorage.setItem("token", encryptPassword)
            sessionStorage.setItem("isWho",item.id)
          }
          flag = true;
        }
      });
      this.$refs[formName].validate((valid) => {
        if (valid && flag) {
          this.$message({
            message: '登录成功！ 欢迎你' + this.ruleForm.number,
            type: 'success'
          });
          this.$router.push("/home")
        } else {
          this.$message({
            message: "登录失败，账号或密码有误",
            type: 'error'
          });
          console.log(valid ,flag)
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },

};
</script>

<style lang="less" scoped>
#bigDiv {
  height: 100%;
}

#mingyan {
  color: rgb(255, 255, 255);
  margin-top: 40px;
  font-size: 17px;
  letter-spacing: 1px;
}

#fanhui {
  float: left;
  font-size: 23px;
  font-weight: bold;
}

#middle {
  // width: 94vw;
  // margin-top: 10px;
  padding-right: 30px;
}

#tittle {
  font-size: 20px;
  margin-top: 10px;
  font-weight: bold;
}

#touxiang {
  height: 200px;
  width: 200px;
  border-radius: 50%;
  background-color: white;
  margin: 60px auto;
  margin-bottom: 40px;

  img {
    margin-top: 10px;
    height: 180px;
    width: 180px;
    border-radius: 50%;
  }
}
</style>